package com.trt.exception;

import com.trt.enums.ResponseStatusEnum;
import com.trt.utils.ApiResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

/**
 * 统一异常拦截处理
 * 可以针对异常的类型进行捕获，然后返回json信息到前端
 */
@ControllerAdvice
public class ApiExceptionHandler {

    /**
     * 处理自定义异常
     */
    @ExceptionHandler(CustomException.class)
    @ResponseBody
    public ApiResult returnCustomException(CustomException e) {
        e.printStackTrace();
        return ApiResult.exception(e.getResponseStatusEnum());
    }

    /**
     * 处理图片大小控制的统一异常
     */
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    @ResponseBody
    public ApiResult returnMaxUploadSizeExceededException(MaxUploadSizeExceededException e) {
        e.printStackTrace();
        return ApiResult.errorCustom(ResponseStatusEnum.FILE_MAX_SIZE_ERROR);
    }

}
